package com.fantasy9.gameserver.message;

import com.fantasy9.gameserver.core.Constants;
import com.fantasy9.gameserver.core.ServerException;
import com.fantasy9.gameserver.player.Player;

/**
 * BaseServerMessage's Bytebuffer contains message length
 * You can just call write to do with it
 * @author Tyrael
 *
 */
public abstract class BaseServerMessage extends BaseMessage {

	private Player player;
	
	public void writeToByteBuffer() throws ServerException{
		
		// Write the Message Length as 0, we'll fill it later after the msg is filled
		writeInt(0);
		
		int offset = buf.position();
		
		// Write the Identifier of the Message
		writeInt(this.getId());
		
		// Let's call WriteMessage to Write to ByteBuffer
		writeMessage();
		
		// Now Write the length again
		offset = buf.position() - offset;
		
		buf.putInt(0, offset);
		
		// flip the Buffer so it's ready to write
		buf.flip();
	}

	@Override
	public int getType() {
		return Constants.MSG_GS_TO_CLIENT;
	}

	public Player getPlayer() {
		return player;
	}

	public void setPlayer(Player player) {
		this.player = player;
	}

}
